#!/bin/sh

mntpt=$1
orasid=$2
dbfsPath=$3

if [ "$#" -lt 2 ]; then
 echo "This script needs 2 input parameter"
 echo "usage: recoverRman.sh <Backup location mount path> <New database sid name> <Backup location mount path> "
exit 1
fi

if [ -z "$rmanchannels" ]; then
rmanchannels=4;
fi

allocatech=
releasech=
i=0
while [ $i -lt "$rmanchannels" ]; do
if [ -z "$allocatech" ]; then
allocatech="allocate channel d$i device type disk;"
releasech="release channel d$i;"
else
allocatech=$allocatech"allocate channel d$i device type disk;"
releasech=$releasech"release channel d$i;"
fi
i=`expr $i + 1`
done

if [ ! -f "$dbfsPath" ]; then
  roTBS=''
else
roTBS=`cat $dbfsPath`
fi

if [ ! -z "$roTBS" ]; then
recoverCMD="skip tablespace $roTBS"
else
recoverCMD=""
fi

export ORACLE_SID=$orasid
dbrecover="dbrecover_"$orasid".txt"
echo "******* recovering database *********** "

rmancmd="run
{
$allocatech
catalog start with '$mntpt/archivelog' noprompt;
recover database $recoverCMD;
$releasech
}"

echo $rmancmd

$ORACLE_HOME/bin/rman target / log $dbrecover << EOF
$rmancmd
exit
EOF
